package com.ks.network.aop;

import com.ks.core.exception.BizExceptionEnum;
import com.ks.core.exception.GunsException;
import com.ks.core.util.JsonUtil;
import com.ks.core.util.MD5Util;
import com.ks.core.util.ToolUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author kingdee
 * @date 2020/11/26 14:45
 */
@Aspect
@Component
@Order(1)
public class AuthorAdvice {
//    @Resource
//    private RedisDao redis;
//    /*@Autowired
//    private IUserBaseService userBaseService;*/
//
//    @Pointcut("@annotation(com.ks.network.aop.Author)")
//    private void Author() {
//    }
//
//    @Before("Author()")
//    public void permissionCHeckFirst(JoinPoint joinPoint)throws Throwable{
//        try {
//            RequestAttributes ra = RequestContextHolder.getRequestAttributes();
//            ServletRequestAttributes sra = (ServletRequestAttributes) ra;
//            HttpServletRequest request = sra.getRequest();
//            String authorToken = request.getHeader("AuthorToken");
//            String md5 = MD5Util.convertMD5(MD5Util.convertMD5(authorToken));
//            String token ="";
//            int i = md5.lastIndexOf(":");
//            String substring = md5.substring(0, i);
//            String[] split = md5.split(":");
//            String s = split[2];
//            String value = redis.getValue(s);
//            if (ToolUtil.isNotEmpty(value)){
//                UserBase userBase = JsonUtil.stringToBean(value, new UserBase().getClass());
//                if (ToolUtil.isNotEmpty(userBase)){
//                    String md51 = MD5Util.getMD5(substring + ":" + userBase.getCSalt());
//                    token = substring + ":" +md51;
//                    if (authorToken.equals(token)){
//
//                    }else {
//                        throw new GunsException(BizExceptionEnum.TOKEN_ERROR);
//                    }
//                }
//            }else {
//                throw new GunsException(BizExceptionEnum.TOKEN_ERROR);
//            }
//        }catch (Exception e){
//            throw new GunsException(BizExceptionEnum.TOKEN_ERROR);
//        }
//    }
}
